package com.ljl.org.controller;

import com.ljl.basic.exception.BusinessException;
import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.ExcelUtil;
import com.ljl.basic.util.PageList;
import com.ljl.org.domain.Shop;
import com.ljl.org.domain.ShopAuditLog;
import com.ljl.org.dto.ShopDto;
import com.ljl.org.dto.SmsCodeDto;
import com.ljl.org.query.ShopQuery;
import com.ljl.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口",description = "店铺接口")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //http://localhost:8080/shop/1 + get
    @ApiOperation(value = "查询单个对象")
    @GetMapping("/{id}")

    public Shop findOne(@PathVariable("id")Long id){
        return shopService.findById(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    //http://localhost:8080/shop + 无 + get
    @ApiOperation(value = "查询所有对象")
    @GetMapping
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 接口：分页查询
     * @param query
     * @return
     */
    //http://localhost:8080/shop + {json对象} + post
    @ApiOperation(value = "分页查询")
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    //http://localhost:8080/shop/1 + delete
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.del(id);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }

    /**
     * 接口：添加或修改
     * @param shop
     * @return
     */
    @ApiOperation(value = "添加或修改")
    //http://localhost:8080/shop + {json对象} + put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if(shop.getId() == null){//添加
                shopService.save(shop);
            }else{
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



    /**
     * 接口：批量删除
     * @param
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            //shopService.patchDelete(ids);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
            //return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    /**
     * 接口：店铺入驻
     * @param
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/audit/pass")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/audit/reject")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @GetMapping("/export")
    public  void export(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
    }

    @PostMapping("/importExcel")
    public  void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        for (Shop shop : shops) {
            System.out.println(shop);
        }

    }

    @GetMapping("/echarts")
    public List<ShopDto> echartsData(){
        List<ShopDto> shopDtos = shopService.getCountByState();
        return shopDtos;
    }

    @GetMapping("/active/{id}")
    public  ModelAndView active(@PathVariable("id") Long id){
        Shop shop = shopService.findById(id);
        shop.setState(3);
        shopService.update(shop);
        ModelAndView modelAndView = new ModelAndView("redirect:http://localhost:8081/#/login");
        return modelAndView;
    }


}